// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var Global = require("Global");

cc.Class({
    extends: cc.Component,

    properties: {
        titleLabel: {
            default: null,
            type: cc.Label
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        Global.mySceneCount ++;
        if(this.titleLabel) {
            this.titleLabel.string = "MyScene " + Global.mySceneCount;
        }

        this.loadRes2();

    },

    start () {

    },

    // update (dt) {},

    onBackBtnClick () {
        cc.director.loadScene("game");
    },

    loadRes1() {
        var animation = this.getComponent(cc.Animation);
        cc.loader.loadRes("test_resources/atlas", cc.SpriteAtlas, (err, atlas) => {
            var spriteFrames = atlas.getSpriteFrames();
            
            var clip = cc.AnimationClip.createWithSpriteFrames(spriteFrames, 10);
            clip.name = 'run';
            clip.wrapMode = cc.WrapMode.Loop;

            animation.addClip(clip);
            animation.play('run');
        });
    },

    // 动态加载资源
    loadRes2() {
        cc.loader.loadRes("test_resources/atlas", cc.SpriteAtlas, (err, atlas) => {
            var frame = atlas.getSpriteFrame('sheep_run_0');

            var node = new cc.Node('Sprite');
            var sprite = node.addComponent(cc.Sprite);
            sprite.spriteFrame = frame;
            // 添加节点到当前组件中
            // node.parent = this; // 不能直接使用this，需要使用node属性获取当前组件附加到的节点
            // 参考文档：http://docs.cocos.com/creator/api/zh/classes/Component.html
            node.parent = this.node;
        });
    },
});
